<?php
require_once("conn.php");
function https_request($url)
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $data = curl_exec($curl);
    if (curl_errno($curl)) {
        return 'ERROR ' . curl_error($curl);
    }
    curl_close($curl);
    return $data;
}

if ($_GET) {
    $code = $_GET['code'];
    $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wxb89990bbfaeb09a5&secret=acf5bf81745db10178789d468b8a2b7d&code=" . $code . "&grant_type=authorization_code";
    $data = https_request($url);
    $userinfo = json_decode($data);
    $openid = $userinfo->openid;
}
//处理提现
if ($_POST) {
    $phone = $_POST['phone'];
    $pwd = $_POST['pwd'];
    $openid = $_POST['openid'];
    $user = $mysql->query("select * from `user` WHERE `id` = {$phone}");
    if (!$user) {
        echo "<script>alert('用户名不存在！')</script>";
        echo "<script>location.href='https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxb89990bbfaeb09a5&redirect_uri=http%3A%2F%2Fwww.china-huitou.com%2Fweiapp%2Flogin.php&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect'</script>";
    } else {
        $userinfo = $user[0];
        if ($userinfo['password'] != $pwd) {
            echo "<script>alert('密码错误！')</script>";
            echo "<script>location.href='https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxb89990bbfaeb09a5&redirect_uri=http%3A%2F%2Fwww.china-huitou.com%2Fweiapp%2Flogin.php&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect'</script>";
        } else {
            //如果数据库已经有openid了，不能再次绑定
            $wxid = $mysql->query("select * from `user` WHERE `wxpay`='{$openid}'");
            if($wxid){
                echo "<script>alert('该微信号已经绑定过了！')</script>";
                echo "<script>location.href='./prompt.php'</script>";
            }else{
                $wxpaySql = "update `user` set `wxpay` = '{$openid}' WHERE `id` = {$phone}";
                $res = $mysql->execute($wxpaySql);
                if($res){
                    echo "<script>alert('绑定成功')</script>";
                    echo "<script>location.href='./prompt.php'</script>";
                }
            }
        }
    }
}
?>
<html>
<head>
    <meta charset="UTF-8">
    <title>有妹纸绑定公众号</title>
    <meta name="keywords" content=""/>
    <meta name="description" content=""/>
    <meta name="apple-mobile-web-app-capable" content="yes"/>
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="format-detection" content="telephone=no">
    <meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=no">
    <link rel="stylesheet" type="text/css" href="./assets/css/reset.css"/>
    <link rel="stylesheet" type="text/css" href="./assets/css/login.css"/>
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
    <script>
        function changeSize() {
            var propo = $(window).width() / 640;
            $('html').css('font-size', 40 * propo + 'px');
        }
        changeSize();
        $(window).resize(function () {
            changeSize();
        });
    </script>
</head>
<body>
<div class="wrap">
    <div class="forms">
        <form method="post" name="login">
            <div class="pcl phone clearfix">
                <input name="openid" type="hidden" value="<?= $openid ?>">
                <input name="phone" type="tel" maxlength="11" placeholder="用户ID"/>
                <span class="icon1"></span>
            </div>
            <div class="pcl pwd clearfix">
                <input name="pwd" type="password" placeholder="请输入密码(6-20位字母或数字)"/>
                <span class="icon3"></span>
            </div>
    </div>
    <button id="regs_submit" class="reg" type="submit">登录绑定</button>
    </form>
</div>
</body>
</html>
